﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ZeroBug.EasyFlow.Core
{
    public class SignalBase
    {
        private bool _status = false;
        private bool _isBreak = false;
        private FlowBase _flow;
        [XmlIgnore]
        public bool IsForceBreak = false;
        public bool IsAutoReset { get; set; } = false;
        public SignalBase()
        {
            
        }
        public SignalBase(bool status, bool isAutoReset, FlowBase flow)
            : this()
        {
            _status = status;
            IsAutoReset = isAutoReset;
            this._flow = flow;
        }

        public bool Wait()
        {
            while (!_status && !_isBreak && !IsForceBreak)
            {
                System.Threading.Thread.Sleep(20);
            }
            if (IsForceBreak)
                IsForceBreak = false;
            if (_isBreak)
                return false;
            if (_status && IsAutoReset)
                Reset();
            return true;
        }
        public bool GetStatus()
        {
            return _status;
        }
        public void Set()
        {
            _status = true;
        }
        public void Reset()
        {
            _status = false;
        }
        public void Break()
        {
            _isBreak = true;
        }
        public void ResetBreak()
        {
            _isBreak = false;
        }
        public void SetFlow(FlowBase flow)
        {
            this._flow = flow;
        }
    }
}
